import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import $ from 'jquery';
import MFS from '@/common/mfs_oauth.js';
import util from '@/common/util.js';

export default {
  name: 'Hello',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      spelName: '全部',
      orgName: '',
      area: '',
      term: '',
      weekDay: '',
      spelId: '',
      classNoShow: false,
      pageNo: 1,
      moreBtn: true,
      content: [],
      optionsArea: [],
      propsArea: {
        value: 'value',
        children: 'cities'
      },
      parentCodeArea: '0',
      optionsTerm: [],
      optionsSpelId: [],
      optionsWeekDay: [],
      selectedOptionsArea: [],
      selectedOptionsTerm: [],
      selectedOptionsSpelId: [],
      selectedOptionsWeekDay: []
    }
  },
  mounted () {
    //MFS.initNav({
    //  //中间标题
    //  "title" : "青少年宫",
    //
    //  //左侧按钮
    //  "leftButton" : {
    //    "handler" : "MFS.goBack"//回调函数名
    //  },
    //
    //  //右侧按钮
    //  "rightButton" : {
    //    "text" : "",//若标题右侧需要展示分享等类似链接按钮，需传值
    //    "handler" : "MFS.navRightCallback2"//回调函数名
    //  }
    //});
    //MFS.showNav()
    // alert(this.$route.query.siteId)
    //var s = 'http://cdqsng.hztangong.com/wx/index.html?siteId=531B601029F97174012A0D3D396600B2&wxAvatar=http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTK3Ce64FXVn7fich4PZ1ZShbNJ3dAZn2EEzp3icVCdBib6bNia7MG7LqmPkLibINvgbGQLKuDDvFK2ibqnQ/132&studentId=68237EEF187150A2E055000000000001&openId=oAOMosxBB9l-pVFZFIJ_2EddHBzM#/Hello';
    //var s = window.location.href
    //var arr = s.split('?')
    //var url = arr[1]
    //var theRequest = new Object()
    //var strs = url.split('&')
    //for (var i = 0; i < strs.length; i++) {
    //  theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
    //}
    //var openIds = theRequest.openId.split('#/')
    //var openId = window.sessionStorage.getItem('openId')
    //if (openId == null || openId == '' || openId == 'undefined') {
    //  window.sessionStorage.setItem('openId', openIds[0])
    //  window.sessionStorage.setItem('studentId', theRequest.studentId)
    //  window.sessionStorage.setItem('siteId', theRequest.siteId)
    //  window.sessionStorage.setItem('wxAvatar', theRequest.wxAvatar)
    //}
    //this.siteId = window.sessionStorage.getItem('siteId')
    //this.studentId = window.sessionStorage.getItem('studentId')
    //if (this.studentId == '-1') {
    //  this.studentId = ''
    //}
    let sietId = sessionStorage.getItem('siteId')
    let beginTime = sessionStorage.getItem('beginTime')
    if (beginTime == null || beginTime == '' || beginTime == undefined) {
      this.setting(sietId)
    }
    this.siteId = sietId
    this.getOrgName(sietId)
    this.classList()
    this.changeArea()
    this.termlist()
    this.specialtylist()
    this.weeklist()
  },
  methods: {
    getOrgName(siteId) {
      var _this = this
      http.get(CONSTANT.GETORG+'?orgId='+siteId, {
        orgId: siteId
      }, (res2) => {
        //alert(JSON.stringify(res2))
      _this.orgName = res2.data.name
      })
    },
    setting(siteId) {
      http.post(CONSTANT.SETTING, {
        orgId: siteId
      }, (res2) => {
        let setting = res2.data.initData.setting
        window.sessionStorage.setItem('setId', setting.id)
      window.sessionStorage.setItem('isStudBack', setting.isStudBack)
      window.sessionStorage.setItem('wechatFees', setting.wechatFees)
      window.sessionStorage.setItem('nxFees', setting.nxFees)
      window.sessionStorage.setItem('isNetApply', setting.isNetApply)
      window.sessionStorage.setItem('isChangeClass', setting.isChangeClass)
      window.sessionStorage.setItem('classNoFlag', setting.classNoFlag)
      window.sessionStorage.setItem('wechatModelId', setting.wechatModelId)
      window.sessionStorage.setItem('idCardNeeds', setting.idCardNeed)
      window.sessionStorage.setItem('beginTime', setting.beginTime)
      window.sessionStorage.setItem('endTime', setting.endTime)
      window.sessionStorage.setItem('randomTime', setting.randomTime)
      this.$router.push({path: '/Hello'})
    })
    },
    changeAreaSelected (val) {
      var _this = this
      _this.area = val[0]
      _this.pageNo = 1
      _this.classList()
      for (var n = 0; n < _this.optionsSpelId.length; n++) {
        if (val[0] == _this.optionsSpelId[n].value) {
          _this.spelName = _this.optionsSpelId[n].label
          return
        }
      }
    },
    changeTermSelected (val) {
      var _this = this
      _this.term = val[0]
      _this.pageNo = 1
      _this.classList()
    },
    changeSpelIdSelected (val) {
      var _this = this
      _this.spelId = val[1]
      _this.pageNo = 1
      _this.classList()
    },
    changeWeekDaySelected (val) {
      var _this = this
      _this.weekDay = val[0]
      _this.pageNo = 1
      _this.classList()
    },
    lookmore () {
      var _this = this
      if (_this.pageNo >= 1) {
        _this.pageNo++
        this.classList()
      }
    },
    classList() {
      var _this = this
      console.log(_this.siteId)
      http.post(CONSTANT.CLASSLISTDO, {
        orgId: _this.siteId,
        area: _this.area,
        term: _this.term,
        spelId: _this.spelId,
        weekDay: _this.weekDay,
        pageNo: _this.pageNo,
        userId: _this.studentId
      }, (res) => {
        let data = res.data
        console.log('classList', data.rows)
      if (data.rows != null && data.rows.length > 0) {
        _this.classListResultHandle(data)
      } else {
        if (_this.pageNo > 1) {
          alert('没有更多了！')
          _this.pageNo--
          return
        } else {
          _this.content = []
        }
      }
    })
    },
    classListResultHandle(data) {
      let _this = this
      let date = new Date().getTime()
      let setting = {
        randomTime: window.sessionStorage.getItem('randomTime'),
        beginTime: window.sessionStorage.getItem('beginTime'),
        endTime: window.sessionStorage.getItem('endTime')
      }
      data.rows.forEach(item => {
        if (item.bean.isUseAge == 1) {
        item.bean.ageMin = util.dateFormat(item.bean.ageMin,false)
        item.bean.ageMax = util.dateFormat(item.bean.ageMax,false)
      }
      _this.processStatus(item, setting, date)
    })
      if (_this.pageNo == 1) {
        _this.content = data.rows
      } else if (_this.pageNo > 1) {
        _this.content = _this.content.concat(data.rows)
      }
    },
    handleItemChangeArea (val) {
      var _this = this
      // 地区选择 动态获取
      if (_this.parentCodeArea != val[0]) {
        _this.parentCodeArea = val[0]
        _this.changeArea()
      }
    },
    processStatus(data, setting, currentTime) {
      if (data.bean.status == 3 && (data.bean.issueStatus == 1 || data.bean.issueStatus == 4)) {
        if (data.bean.recruitNum - data.bean.reserveNum > data.bean.applyUsedNum) {
          data.canApply = true;
          data.statusProperty = "报名进行中";
        } else {
          data.canApply = false;
          data.statusProperty = "名额已满";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 2) {
        data.canApply = false;
        if (data.bean.enrollType == 0) {
          data.statusProperty = "即报即得";
        } else if (data.bean.enrollType == 1) {
          data.statusProperty = "摇号中";
        } else if (data.bean.enrollType == 2) {
          data.statusProperty = "面试中";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 3) {
        data.canApply = false;
        data.statusProperty = "报名已关闭";
      }
      if (setting.beginTime > currentTime) {
        data.canApply = false;
        data.statusProperty = "报名未开始";
      }
      if (setting.endTime < currentTime) {
        data.canApply = false;
        data.statusProperty = "报名已结束";
      }
      // 是否已经到了预设的摇号时间
      if (setting.randomTime < currentTime) {
        // 摇号的班级进入摇号时间后不得再报名
        if (data.bean.enrollType == 1) {
          data.canApply = false;
          data.statusProperty = "摇号中";
        }
      }
    },
    changeArea () {
      // 区域 改成了 专业
      var _this = this
      http.post(CONSTANT.SPECIALTYLIST, {orgId: _this.siteId}, (res) => {
        res.data.forEach(item => {
        if (!item.parentId && item.status == '1') {
        var areaFirst = {}
        areaFirst.label = item.name
        areaFirst.value = item.id
        areaFirst.cities = []
        _this.optionsArea.push(areaFirst)
      }
    })
      res.data.forEach(item => {
        if (item.parentId) {
        _this.optionsArea.forEach(pItem => {
          if (item.parentId == pItem.value) {
          var areaSecond = {}
          areaSecond.label = item.name
          areaSecond.value = item.id
          pItem.cities.push(areaSecond)
        }
      })
      }
    })
    })
    },
    termlist() {
      // 学期
      var _this = this
      http.post(CONSTANT.ORGDICTLIST, {
        orgId: _this.siteId,
        type: 'edu_term'
      }, (res) => {
        res.data.forEach(item => {
        var termFirst = {}
        termFirst.label = item.label
      termFirst.value = item.value
      _this.optionsTerm.push(termFirst)
    })
    })
    },
    specialtylist () {
      // 专业 改成了  区域
      var _this = this
      http.post(CONSTANT.ORGDICTLIST, {orgId: _this.siteId,type: 'edu_business_rea'}, (res) => {
        console.log('quyu', res)
        let data = res.data
        //_this.spelName = res[0].resName
        for (var s = 0; s < data.length; s++) {
          var termFirst = {}
          termFirst.label = data[s].label
          termFirst.value = data[s].value
          _this.optionsSpelId.push(termFirst)
        }
      })
    },
    weeklist() {
      // 时间段
      var _this = this
      http.post(CONSTANT.DICTLIST, {
        orgId: _this.siteId,
        type: 'sys_weekday'
      }, (res) => {
        res.data.forEach(item => {
        var weekFirst = {}
        weekFirst.label = item.label
      weekFirst.value = item.value
      _this.optionsWeekDay.push(weekFirst)
    })
    })
    },
    goCourseDetails (id,canApply) {
      window.sessionStorage.setItem('event_id', id)//courseDetails
      this.studentId = window.sessionStorage.getItem('studentId')
      if (this.studentId == '-1') {
        this.$router.push({path: '/login'})
      } else {
        if (canApply) {
          this.$router.push({path: '/courseDetails'})
        }
      }
    }
  }
}
